package com.dcsgo.socketPlay.client;

import com.dcsgo.socketPlay.client.game.Game;
import com.dcsgo.socketPlay.client.game.GamePanel;
import com.dcsgo.socketPlay.common.Ball;
import com.dcsgo.socketPlay.common.Message;
import com.dcsgo.socketPlay.common.MessageType;

import java.io.*;
import java.net.Socket;

/**
 * @author DCSGO
 * @version 1.0
 * 提供服务的类
 */
public class Services {
    private static Socket socket;
    private static Integer clientId;
    private static Long lastMesCnt = 0L;

    public static void offline() {
        try {
            socket.close();
            GamePanel.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("下线了!");
        }
    }

    public static void sendClientOffline() {
        Message sendMes = new Message();
        sendMes.setMesType(MessageType.CLIENT_OFFLINE);
        try {
            sendMessage(sendMes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void load() throws IOException, ClassNotFoundException {
        System.out.println("正在读取配置信息...");
        String serverIP = Client.getConnectiveProperty("serverIP");
        String serverPort = Client.getConnectiveProperty("serverPort");
        System.out.println("Services初始信息配置完成!");
        int port = Integer.parseInt(serverPort);

        System.out.println("正在向服务器建立连接...");
        socket = new Socket(serverIP, port);
        Message sendMes = new Message();
        sendMes.setMesType(MessageType.ESTABLISH_CONNECTION);
        sendMessage(sendMes);
        Message receiveMes = receiveMessage();

        if (receiveMes == null) {
            System.out.println("连接失败!");
            System.exit(0);
        }

        if (receiveMes.getMesType().equals(MessageType.CONNECTED_SUCCESSFULLY)) {
            clientId = receiveMes.getReceiveClientId();
            Game.setGameProperties(receiveMes.getProperties());
            GamePanel.setReplaceBall(receiveMes.getReplaceBall());
            GamePanel.setBalls(receiveMes.getBallList());
            System.out.println("已成功与服务器建立连接!");
        } else {
            System.out.println("连接建立失败!");
            throw new RuntimeException("获取 CONNECTED_SUCCESSFULLY 失败!");
        }
    }

    public static Message receiveMessage() throws IOException, ClassNotFoundException {
        if (!socket.isClosed()) {
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message receiveMes = (Message) ois.readObject();
            System.out.println(receiveMes.getMesCnt() - lastMesCnt);
            if (lastMesCnt < receiveMes.getMesCnt()) {
                lastMesCnt = receiveMes.getMesCnt();
                return receiveMes;
            } else {
                return null;
            }
        } else {
            throw new RuntimeException("不能再接收消息了，因为socket已经关闭!");
        }
    }

    public static void sendPlayerOperation(Ball replaceBall) {
        Message message = new Message();
        message.setMesType(MessageType.PLAYER_OPERATION);
        message.setReplaceBall(replaceBall);
        try {
            Services.sendMessage(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void sendMessage(Message message) throws IOException {
        if (!socket.isClosed()) {
            message.setMesCnt(System.currentTimeMillis());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
        } else {
            throw new RuntimeException(message.getMesType() + " 发送失败！socket已经关闭!");
        }
    }

    public static Integer getClientId() {
        return clientId;
    }

}
